// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem59.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=59 lang=cpp
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int> > res(n);
        for (auto &row : res) {
            row.resize(n);
        }
        int top = 0;
        int left = 0;
        int bottom = n - 1;
        int right = n - 1;
        int num = 0;
        while (num < n * n) {
            for (int i = left; i <= right && num < n*n; i++) {
                num++;
                res[top][i] = num;
            }
            top++;

            for (int i = top; i <= bottom && num < n*n; i++) {
                num++;
                res[i][right] = num;
            }
            right--;

            for (int i = right; i >= left && num < n*n; i--) {
                num++;
                res[bottom][i] = num;
            }
            bottom--;

            for (int i = bottom; i >= top && num < n*n; i--) {
                num++;
                res[i][left] = num;
            }
            left++;
        }
        return res;
    }
};
// @lc code=end

